package com.xiaotao.consumer;

import com.rabbitmq.client.Channel;
import com.xiaotao.entity.Order;
import com.xiaotao.mapper.OrderMapper;
import com.xiaotao.util.ConstantUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Component
@Slf4j
public class OrderDelayConsumer {
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private OrderMapper orderMapper;
    @RabbitListener(queues = {ConstantUtil.QUEUE_DELAY_CHECK_ORDER})
    public void consume(String msg, Channel channel, Message message){
        log.info("消费者收到消息：{}",msg);
        try {
            Order order = orderMapper.findByOrderNum(msg);
            if (order.getStatus()==1){
                order.setStatus(4);
                orderMapper.updateStatus(order);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }

    }
}
